package com.kele.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kele.domain.Blog;
import com.kele.domain.User;
import com.kele.service.BlogService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/blogs")
public class BlogsController {

    @Resource
    private BlogService blogService;

    /**
     * 查询所有博客
     * @param model
     * @param pageNums
     * @return
     */
    @RequestMapping(value = "/findBlogs",method = RequestMethod.GET)//已经实现
    public String findBlogs(Model model,
                            @RequestParam(value = "pageNums",defaultValue = "1") int pageNums){
        PageHelper.startPage(pageNums,5);
        List<Blog> blogs = blogService.findBlogs();
        PageInfo pageInfo=new PageInfo(blogs,5);
        model.addAttribute("pageInfo",pageInfo);
        return "static_resources/templates/article";
    }

    /**
     * 查看更多博客
     * @param model
     * @return
     */
    @RequestMapping(value = "/checkBlog",method = RequestMethod.GET)
    public String checkBlog(Model model){
        List<Blog> blogs = blogService.findBlogs();
        model.addAttribute("all",blogs);
        return "static_resources/templates/checkBlog";
    }

    /**
      *@author 赵奥林
      *@description 2021/5/27 23:19 根据博客名查询博客信息
     * @param model
     * @param field
     * @return java.lang.String
      */
    @RequestMapping("/findByTitle")
    public String findByTitle(Model model,String field){
        System.out.println(field);
        List<Blog> blogs=blogService.findByTitle(field);//执行根据名字查询的方法
        System.out.println("blogs"+blogs);
        model.addAttribute("blog",blogs);
        return "/static_resources/templates/search";
    }

    /**
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/findById")
    public String findById(int id,Model model){
        Blog blog = blogService.queryById(id);
        blogService.addDoh(blog.getBid());
        //展示该博主的文章数量
       int count= blogService.countArticle(blog.getBlogger());
       model.addAttribute("count",count);
        model.addAttribute("blog",blog);
        System.out.println(blog.getBlogger()+"下的博客文章数量为"+count);
        return "static_resources/templates/read";
    }


   /**
     *@author 赵奥林
     *@description 2021/5/27 23:20  //根据id删除博客文章
    * @param id
    * @return java.lang.String
     */
    @RequestMapping(value = "/deleteBlog",method = RequestMethod.GET)
    //RequestParam是获取form表单中的值,最后提交返回,但是删除,不是用的form表单返回,所以不用RequestParam("id")
    public String deleteBlog(int id, HttpServletRequest request){
        blogService.deleteBlog(id);
        int pageNum= Integer.parseInt(request.getParameter("pageNum"));
        int total= Integer.parseInt(request.getParameter("total"));
        if (total-1<=0){
            pageNum=pageNum-1;
            if (pageNum==0){
                pageNum=1;
            }
        }
        System.out.println(total);
        return "redirect:/blogs/findBlogs?pageNums="+pageNum;
    }

    /**
      *@author 赵奥林
      *@description 2021/5/27 23:20 //添加点赞（+1）
     * @param bid
     * @return java.lang.String
      */
    @RequestMapping(value = "/addthumbsUp",method = RequestMethod.POST)
    public String addthumbsUp(int bid){
        blogService.addThumbsUp(bid);
        return "redirect:/findBlogs";
    }

    /**
      *@author 赵奥林
      *@description 2021/5/27 23:20 //取消点赞（-1）
     * @param id
     * @return java.lang.String
      */
    @RequestMapping(value = "/subthumbsUp",method = RequestMethod.GET)
    public String subthumbsUp(int id){
        blogService.subThumbsUp(id);
        return "1";
    }



    /**
      *@author 赵奥林
      *@description 2021/5/27 23:20 //实现点击博客进入后浏览量(热度)+1
     * @param id
     * @return java.lang.String
      */
    @RequestMapping(value = "/addDoh",method = RequestMethod.GET)
    public String addDoh(int id){
        blogService.addDoh(id);
        return "redirect:/findBlogs";
    }

    /**
     * 发布博客
     * @param blog
     * @param model
     * @return
     */
    @RequestMapping(value = "/releaseBlog",method = RequestMethod.POST)
    public String releaseBlog(Blog blog,Model model){
        blogService.addBlog(blog);
        return "redirect:/findBlogs";
    }

    /**
     * 展示博客到主页面上
     * @param model
     * @return
     */
    @RequestMapping(value = "showBlogs",method = RequestMethod.GET)
    public String showBlogs(Model model){

        List<Blog> blogs = blogService.showBlogs();
        System.out.println(blogs);

        //新的list集合储存取出来的数据
        List<Blog> list=new ArrayList<Blog>();

        Random random=new Random();
        for (int i = 0; i < 6; i++) {
            int intRandom=random.nextInt(blogs.size()-1);
            list.add(blogs.get(intRandom));
            blogs.remove(blogs.get(intRandom));
        }
        System.out.println(list);
        System.out.println(list.get(1));
        System.out.println(list.get(1).getBid());

        model.addAttribute("blogs",list);
        return "static_resources/templates/index";
    }



    /**
     * 展示博客到博主管理
     * @param model
     * @return
     */
    @RequestMapping(value = "findBlogsByBlogger",method = RequestMethod.GET)
    public String findBlogsByBlogger(Model model,HttpServletRequest request){
        User user =(User) request.getSession().getAttribute("User");
        String blogger= user.getNickname();
        System.out.println(blogger);
        List<Blog> blogs = blogService.findBlogsByBlogger(blogger);
        System.out.println(blogs);
        model.addAttribute("people",blogs);
        return "static_resources/templates/contentManagement";
    }




}
